package fx.mine.active.component;

import io.github.palexdev.materialfx.font.MFXFontIcon;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class TitleBar extends HBox {
    private double xOffset;
    private double yOffset;
    private final Stage stage;

    public TitleBar(Stage stage) {

        this.stage = stage;

        setId("windowHeader");
        setPrefSize(1280, 50);
        setSpacing(10);
        setAlignment(Pos.CENTER_RIGHT);
        setPadding(new Insets(0, 10, 0, 0));

        AnchorPane.setLeftAnchor(this, 0.0);
        AnchorPane.setRightAnchor(this, 0.0);
        AnchorPane.setTopAnchor(this, 0.0);

        addAlwaysOnTopIcon();
        addMinimalIcon();
        addCloseIcon();


        getStylesheets().add(
                getClass().getResource("/css/titlebar.css").toExternalForm()
        );

        setOnMousePressed(mouseEvent -> {
            xOffset = stage.getX() - mouseEvent.getScreenX();
            yOffset = stage.getY() - mouseEvent.getScreenY();
//            log.info("window header press x = {} , y = {}", xOffset, yOffset);
        });

        setOnMouseDragged(mouseEvent -> {
            double x = mouseEvent.getScreenX() + xOffset;
            double y = mouseEvent.getScreenY() + yOffset;
            stage.setX(x);
            stage.setY(y);
//            log.info("window header drag x = {} , y = {}", x, y);
        });
    }

    private void addCloseIcon() {
        ObservableList<Node> children = getChildren();
        var alwaysOnTopIcon = new MFXFontIcon("mfx-circle", 15.0);
        alwaysOnTopIcon.getStyleClass().add("close");
        alwaysOnTopIcon.setOnMouseClicked(mouseEvent -> {
            Platform.exit();
        });
        children.add(alwaysOnTopIcon);
    }

    private void addMinimalIcon() {
        ObservableList<Node> children = getChildren();
        var minimal = new MFXFontIcon("mfx-circle", 15.0);
        minimal.getStyleClass().add("minimal");
        minimal.setOnMouseClicked(mouseEvent -> {
            stage.setMaximized(true);
            stage.setIconified(true);
        });
        children.add(minimal);
    }

    private void addAlwaysOnTopIcon() {
        ObservableList<Node> children = getChildren();
        var alwaysOnTopIcon = new MFXFontIcon("mfx-circle", 15.0);
        alwaysOnTopIcon.getStyleClass().add("always-on-top-icon");
        alwaysOnTopIcon.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            boolean newVal = !stage.isAlwaysOnTop();
            alwaysOnTopIcon.pseudoClassStateChanged(PseudoClass.getPseudoClass("always-on-top-icon"), newVal);
            stage.setAlwaysOnTop(newVal);
        });

        children.add(alwaysOnTopIcon);
    }
}
